/*
===========================================================================
Copyright (C) 2013-2014 Avotu Briezhaudzetava

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.

===========================================================================
*/

#ifndef LOGENTRY_H
#define LOGENTRY_H

//
// includes
//
#include "databaseentry.h"
#include "common.h"
#include <QDateTime>

//
// class: LogEntry
//
class LogEntry : public DatabaseEntry {
    Q_PROPERTY( int precursorId READ precursorId WRITE setPrecursorId )
    Q_PROPERTY( int userId READ userId WRITE setUserId )
    Q_PROPERTY( float amount READ amount WRITE setAmount )
    Q_PROPERTY( QString date READ date WRITE setDate )
    Q_PROPERTY( QString purpose READ purpose WRITE setPurpose )
    Q_PROPERTY( Common::Units units READ units WRITE setUnits )

public:
    explicit LogEntry( const QSqlRecord &record, const QString &table );
    ~LogEntry();

    int precursorId() const { return this->record().value( "precursorId" ).toInt(); }
    int userId() const { return this->record().value( "userId" ).toInt(); }
    float amount() const { return this->record().value( "amount" ).toFloat(); }
    float amountTo( Common::Units baseUnits, float density ) const;// { return this->record().value( "amount" ).toFloat(); }
    QString date() const { return this->record().value( "date" ).toString(); }
    QDateTime dateTime() const { return QDateTime::fromString( this->date(), Qt::TextDate ); }
    QString purpose() const { return this->record().value( "purpose" ).toString(); }
    Common::Units units() const { return static_cast<Common::Units>( this->record().value( "units" ).toInt()); }

public slots:
    void setPrecursorId( int precursorId ) { this->setValue( "precursorId", precursorId ); }
    void setUserId( int userId ) { this->setValue( "userId", userId ); }
    void setAmount( float amount ) { this->setValue( "amount", amount ); }
    void setDate( const QString &date ) { this->setValue( "date", date ); }
    void setDateTime( const QDateTime &date ) { this->setValue( "date", date.toString( Qt::TextDate )); }
    void setPurpose( const QString &purpose ) { this->setValue( "purpose", purpose ); }
    void setUnits( Common::Units units ) { this->setValue( "units", static_cast<int>( units )); }

};

#endif // LOGENTRY_H

